import numpy as np
import json

class AutopilotData:
    def __init__(self,data_file="data/data.txt",label_file="data/label.txt"):
        self.data_file = data_file
        self.label_file = label_file
        
    def load_data(self):
        datas = self.get_data(self.data_file)
        labels = self.get_data(self.label_file)
        return self.shuffle(datas,labels)
        # return datas,labels

    #打乱数据的顺序
    def shuffle(self,datas,labels):
        index = [i for i in range(len(datas))]
        np.random.shuffle(index)
        datas = datas[index]
        labels = labels[index]
        # temp = np.array([datas, labels])
        # temp = temp.transpose()
        # np.random.shuffle(temp)
        # datas = temp[:, 0]     # array of images   (N,)
        # labels = temp[:, 1]
        return datas,labels
    
    def get_data(self,file_path):
        data = []
        with open(file_path, 'r') as file:
            while True:
                str_data = file.readline()
                if str_data:
                    res=json.loads(str_data)
                    data.append(np.array(res,np.float64))
                else:
                    break;
        return np.array(data)
    
    #分割测试和训练数据集
    def split_data(self,datas,labels,train_rate=0.8):
        data_len = len(datas)
        train_len = int(data_len*train_rate)

        #提取训练和测试数据
        train_data = datas[0 : train_len, : ]
        train_label = labels[0 : train_len, : ]
        
        test_data = datas[train_len : data_len, : ]
        test_label = labels[train_len : data_len, : ]
        
        return train_data,train_label,test_data,test_label